package com.jeesite.modules.inventory.web;

import java.util.List;

import com.jeesite.common.utils.OrgUtil;
import com.jeesite.modules.inventory.entity.constant.InventoryConstant;
import com.jeesite.modules.inventory.entity.qo.InventoryQO;
import com.jeesite.modules.inventory.entity.vo.InventoryVO;
import com.jeesite.modules.inventoryclass.entity.InventoryClass;
import com.jeesite.modules.inventoryclass.service.InventoryClassService;
import com.jeesite.modules.warehouse.service.WarehouseService;
import com.jeesite.modules.warehouseposition.service.WarehousePositionService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.inventory.entity.Inventory;
import com.jeesite.modules.inventory.service.InventoryService;

/**
 * 存货档案Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/inventory/inventory")
public class InventoryController extends BaseController {

	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private InventoryClassService inventoryClassService;
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private WarehousePositionService warehousePositionService;


	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Inventory get(Long autoId, boolean isNewRecord) {
		return inventoryService.get(autoId, isNewRecord);
	}

	@RequiresPermissions("inventory:inventory:view")
	@RequestMapping(value = "index")
	public String index(InventoryClass inventoryClass, Model model) {
		model.addAttribute("inventoryClass", inventoryClass);
		model.addAttribute("ctrlPermi", Global.getConfig("user.adminCtrlPermi", "2"));
		return "modules/inventory/inventoryIndex";
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("inventory:inventory:view")
	@RequestMapping(value = {"list", ""})
	public String list(Inventory inventory, Model model) {
		model.addAttribute("inventory", inventory);
		return "modules/inventory/inventoryList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("inventory:inventory:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<InventoryVO> listData(InventoryQO inventoryQO, HttpServletRequest request, HttpServletResponse response) {
		inventoryQO.setPage(new Page<>(request, response));
		Page<InventoryVO> page = inventoryService.findInventoryPage(inventoryQO);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("inventory:inventory:view")
	@RequestMapping(value = "form")
	public String form(Inventory inventory, Model model) {
		model.addAttribute("inventory", inventory);
		if (OrgUtil.isValid()){
			inventory.setOrgId(OrgUtil.getOrgId());
			inventory.setOrgCode(OrgUtil.getOrgCode());
			inventory.setOrgName(OrgUtil.getOrgName());
			inventory.setSourceType(InventoryConstant.SOURCE_TYPE_MANUAL);
		}
		model.addAttribute("inventoryClassList", inventoryClassService.selectInventoryClassList());
		model.addAttribute("warehouseList", warehouseService.selectWarehouseList());
		model.addAttribute("warehousePositionList", warehousePositionService.selectWarehousePositionList());
		return "modules/inventory/inventoryForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("inventory:inventory:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Inventory inventory) {
		inventoryService.save(inventory);
		return renderResult(Global.TRUE, text("保存存货档案成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("inventory:inventory:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(Inventory inventory) {
		inventory.setStatus(Inventory.STATUS_DISABLE);
		inventoryService.updateStatus(inventory);
		return renderResult(Global.TRUE, text("停用存货档案成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("inventory:inventory:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(Inventory inventory) {
		inventory.setStatus(Inventory.STATUS_NORMAL);
		inventoryService.updateStatus(inventory);
		return renderResult(Global.TRUE, text("启用存货档案成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("inventory:inventory:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Inventory inventory) {
		inventoryService.delete(inventory);
		return renderResult(Global.TRUE, text("删除存货档案成功！"));
	}
	
}